package com.dlwx.parttimetalent.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.SalesManBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.SeleteAddressInter;
import com.dlwx.parttimetalent.utils.AddressPopuWindow;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.LogUtils;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SalesmanActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_content)
    TextView tvContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_salesman);
        ButterKnife.bind(this);
        initView();
        getDatas();
    }


    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("约神平台诚邀精英加入");
    }

    private void getDatas() {
        OkgoUtil.post(Http.jztxExplain, this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        SalesManBean parentBean = DealResult.getInstance().dealDatas(SalesmanActivity.this, response, new TypeToken<SalesManBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            SalesManBean.BodyBean bean = parentBean.getBody();
                            if(bean==null){return;}
                               String content= bean.getTextcontent();
                            if(TextUtils.isEmpty(content)){return;}
                            tvContent.setText(content.replaceAll("#","\n"));
                        } else {
                        }
//                        ToastUtils.toastForShort(SalesmanActivity.this, parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                    }
                });
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_confirm:
                startPopup();
                break;
        }
    }

    private void startPopup() {
        AddressPopuWindow.showWindow(ctx, tvConfirm);
        AddressPopuWindow.setData(new SeleteAddressInter() {
            @Override
            public void setAddressSeleteData(String pro, String c, String a) {
//                province = pro;
//                city = c;
//                area = a;
//                tvAddress.setText(pro + c + a);
                if (TextUtils.isEmpty(c)) {
                    c = pro;
                }
                LogUtils.d("pro:" + pro + "-city:" + c + "area:" + a);
                confirm(pro, c, a);
            }
        });
    }

    private void confirm(String pro, String c, String a) {
        loadingDialog.show();
        OkgoUtil.post(Http.SubSignUpInfo, this)
                .params("province", pro)
                .params("city", c)
                .params("area", a)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(SalesmanActivity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {

                        } else {
                        }
//                        ToastUtils.toastForShort(SalesmanActivity.this, parentBean.getResult());
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                    }
                });
    }
}
